﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using System.IO;

namespace JTicket.Printer
{
    public class Print
    {
        /// <summary>
        /// 销售打印ＳＱＬ命令
        /// </summary>
        /// <param name="Barcode">条码</param>
        /// <param name="Sixid">座位编号</param>
        /// <param name="Titlecode">剧目编号</param>
        /// <param name="Fpbh">发票序号</param>
        /// <returns></returns>
        public static string MakeSaleCmd(string Barcode, string Sixid, string Titlecode, string ThCode)
        {
            string SQL = string.Format("select top 1 '条形码'=barcode,'演出剧目'=TitleName,'演出单位'=Steam,'演出日期'=yandate,'演出类别'=Stitlesort,'演出时间'=yantime,'剧场名字'=ThName,'座位区域'=Area,'座位编号'=Sixid,'座位描述'=MSg,'票类名称'=Ticketname,'门票单价'=Price,'座位排号'=Pnum,'座位座号'=Znum,'场次号'=TitleNum,'发票号'=InvoiceCode  from H_PrintBal  where barcode like '{0}' and  Sixid like '{1}' and  TitleCode like '{2}' and ThCode like '{3}'", Barcode, Sixid, Titlecode, ThCode);
            return SQL;
        }
        /// <summary>
        /// 预订取票SQL命令
        /// </summary>
        /// <param name="OrderID"></param>
        /// <param name="Titlecode"></param>
        /// <param name="ThCode"></param>
        /// <returns></returns>
        public static string MakeOrderGetCmd(string OrderID, string Titlecode, string ThCode,string BarCode)
        {
            string SQL = string.Format("select top 1 '条形码'=barcode,'演出剧目'=TitleName,'演出单位'=Steam,'演出日期'=yandate,'演出类别'=Stitlesort,'演出时间'=yantime,'剧场名字'=ThName,'座位区域'=Area,'座位编号'=Sixid,'座位描述'=MSg,'票类名称'=Ticketname,'门票单价'=Price,'座位排号'=Pnum,'座位座号'=Znum,'场次号'=TitleNum,'发票号'=InvoiceCode  from H_PrintBal  where Flag like '{0}' and  TitleCode like '{1}' and ThCode like '{2}' and barCode ='{3}'", OrderID, Titlecode, ThCode, BarCode);
            return SQL;
        }
        /// <summary>
        /// 重打票SQL命令
        /// </summary>
        /// <param name="BarCode">条码</param>
        /// <param name="Area">区域</param>
        /// <param name="Pnum">排号</param>
        /// <param name="znum">座位号</param>
        /// <param name="Titlecode">剧目编号</param>
        /// <param name="ThCode">剧院编号</param>
        /// <returns></returns>
        public static string MakeRePrintCmd(string BarCode, string Titlecode, string Opercode)
        {
            string SQL = string.Format("select top 1 '条形码'=barcode,'演出剧目'=TitleName,'演出单位'=Steam,'演出日期'=yandate,'演出类别'=Stitlesort,'演出时间'=yantime,'剧场名字'=ThName,'座位区域'=Area,'座位编号'=Sixid,'座位描述'=MSg,'票类名称'=Ticketname,'门票单价'=Price,'座位排号'=Pnum,'座位座号'=Znum,'场次号'=TitleNum,'发票号'=InvoiceCode  from H_PrintBal  where barcode like '{0}' and  TitleCode like '{1}' and OperCode like '{2}'", BarCode, Titlecode, Opercode);
            return SQL;

        }
        /// <summary>
        /// 整比重打印(未实现)
        /// </summary>
        /// <param name="Flag"></param>
        /// <param name="TitleCode"></param>
        /// <param name="ThCode"></param>
        /// <returns></returns>
        public static List<string> MakeRePrintCmdList(string Flag, string TitleCode, string ThCode)
        {
            List<string> CmdList = new List<string>();
            return CmdList;
        }
        /// <summary>
        /// 打印数据集模板
        /// </summary>
        /// <returns></returns>
        public static string MakePrintModel()
        {
            string SQL = "select top 1 '条形码'='123456789','演出剧目'='演出剧目','演出单位'='演出单位','演出日期'='演出日期','演出类别'='演出类别','演出时间'='演出时间','剧场名字'='剧场名字','座位区域'='座位区域','座位编号'='座位编号','座位描述'='座位描述','票类名称'='票类名称','门票单价'='门票单价','座位排号'='座位排号','座位座号'='座位座号','场次号'='场次号','发票号'='123456789'";
            return SQL;
        }


        public static void BuildTaxU(string kpje, string kpr, int count)
        {
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            BLL.D_Tax bllTax = new BLL.D_Tax();
            Model.D_Tax Tax = bllTax.GetModel(bllOper.Oper().ThCode);
            try
            {
                if (string.IsNullOrEmpty(kpr))
                    kpr = "无";
                string FileName = string.Format("{0}{1}{2}{3}",
                    DateTime.Now.ToString("yyyyMMdd"), "U", Tax.swbh, ".xml");
                string FilePath = HttpContext.Current.Server.MapPath("XmlFile\\" + FileName + "");
                StringBuilder sb = new StringBuilder();
                DataSet dataSet = new DataSet();
                DataRow dr;
                dataSet.ReadXmlSchema(HttpContext.Current.Server.MapPath("App_Data\\InvoiceUseInfo.xsd"));
                if (File.Exists(FilePath))
                    dataSet.ReadXml(FilePath);
                //表ROOT
                dr = dataSet.Tables["ROOT"].NewRow();
                dr["ROOT_Id"] = Tax.fpsy;
                dataSet.Tables["ROOT"].Rows.Add(dr);
                //表Nsritem
                dr = dataSet.Tables["Nsritem"].NewRow();
                dr["Nsrsbh"] = Tax.swbh;
                dr["Nsrmc"] = Tax.qymc;
                dr["Ssqq"] = DateTime.Now.ToString("yyyy-MM-dd");
                dr["Ssqz"] = DateTime.Now.ToString("yyyy-MM-dd");
                dr["Rq"] = DateTime.Now.ToString("yyyy-MM-dd");
                dr["Nsritem_Id"] = Tax.fpsy;
                dr["ROOT_Id"] = Tax.fpsy;
                dataSet.Tables["Nsritem"].Rows.Add(dr);
                //表Fplist
                dr = dataSet.Tables["Fplist"].NewRow();
                dr["Fplist_Id"] = Tax.fpsy;
                dr["Nsritem_Id"] = Tax.fpsy;
                dataSet.Tables["Fplist"].Rows.Add(dr);
                //======================================================
                //表Fpinfo
                dr = dataSet.Tables["Fpinfo"].NewRow();
                dr["InitFpdm"] = Tax.Fpdm;
                dr["Fpdm"] = Tax.Fpdm;
                dr["Fpdmext"] = Tax.Fpdm;
                string st = Tax.fpsy;
                //更新当前使用发票代码
                BLL.D_Tax.UpdateSY(bllOper.Oper().ThCode);
                dr["Fphm"] = st;
                dr["Zfbz"] = "0";
                dr["Kprq"] = DateTime.Now.ToString("yyyy-MM-dd");
                dr["Kpje"] = kpje;
                dr["Kpr"] = kpr;
                dr["Fpinfo_Id"] = Tax.fpsy;
                dr["Fplist_Id"] = Tax.fpsy;
                dataSet.Tables["Fpinfo"].Rows.Add(dr);
                //表Fkf
                dr = dataSet.Tables["Fkf"].NewRow();
                dr["Fkfnsrsbh"] = "无";
                dr["Fkfnrsmc"] = Tax.qymc;
                dr["Fkfaddr"] = "无";
                dr["Fpinfo_Id"] = Tax.fpsy;
                dataSet.Tables["Fkf"].Rows.Add(dr);
                //表Skf
                dr = dataSet.Tables["Skf"].NewRow();
                dr["Skfnsrsbh"] = Tax.swbh;
                dr["Skfnrsmc"] = Tax.qymc;
                dr["Skr"] = kpr; ;
                dr["Fpinfo_Id"] = Tax.fpsy;
                dataSet.Tables["Skf"].Rows.Add(dr);
                dataSet.Tables["Fplist"].AcceptChanges();
                dataSet.Tables["Fpinfo"].AcceptChanges();
                dataSet.Tables["Fkf"].AcceptChanges();
                dataSet.Tables["Skf"].AcceptChanges();
                dataSet.AcceptChanges();
                //======================================================
                sb.Append("<?xml version=\"1.0\"  encoding=\"GBK\" standalone=\"yes\"?>");
                sb.Append(dataSet.GetXml());
                File.WriteAllText(FilePath, sb.ToString(), Encoding.Default);
            }
            catch
            {

            }
            //dataSet.WriteXml(FilePath);
        }
        public static void BuildTaxS(string kpje, string kpr, int count)
        {
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            BLL.D_Tax bllTax = new BLL.D_Tax();
            Model.D_Tax Tax = bllTax.GetModel(bllOper.Oper().ThCode);
            //纳税人识别号
            string FileName = string.Format("{0}{1}{2}{3}", DateTime.Now.ToString("yyyyMMdd"), "S", Tax.swbh, ".xml");
            string FilePath = HttpContext.Current.Server.MapPath("Xml\\" + FileName + "");
            StringBuilder sb = new StringBuilder();
            DataSet dataSet = new DataSet();
            DataRow dr;
            dataSet.ReadXmlSchema(HttpContext.Current.Server.MapPath("App_Data\\InvoiceSaleInfo.xsd"));
            //表一
            dr = dataSet.Tables[0].NewRow();
            dr["Nsrsbh"] = Tax.swbh;
            dr["Nsrmc"] = Tax.qymc;
            dr["SaleDate"] = DateTime.Now.ToString("yyyy-MM-dd");
            dr["Fpjbr"] = "无";
            dr["Swjbrdm"] = Tax.Swjbrdm; ;
            dr["Swjbrmc"] = Tax.Swjbrmc;
            dr["Ckkfdm"] = "03900001";
            dr["Ckkfmc"] = "税务局直属分局发票窗口1";
            dr["FpSaleInfo_Id"] = "2";
            dataSet.Tables[0].Rows.Add(dr);
            //表二
            dr = dataSet.Tables[1].NewRow();
            dr["SaleInfo_Id"] = "1";
            dr["FpSaleInfo_Id"] = "2";
            dataSet.Tables[1].Rows.Add(dr);

            //表三
            //for (int i = 0; i <= 1; i++)
            //{
            dr = dataSet.Tables[2].NewRow();
            dr["Fpmcdm"] = "037515221006000000783224";
            dr["Fpmc"] = Tax.Fpmc;
            dr["Fplbdm"] = "1";
            dr["Fplbmc"] = "其它";
            dr["Fpdm"] = "30000001";
            dr["Fpdmext"] = "221010659014-4";
            dr["Fpqh"] = "41205551";
            dr["Fpzh"] = "41206550";
            dr["Mbzs"] = "50";
            dr["Dw"] = "张";
            dr["Bs"] = "2";
            dr["Dj"] = "150";
            dr["Je"] = "300";
            dr["SaleInfo_Id"] = "1";
            dataSet.Tables[2].Rows.Add(dr);
            //}
            sb.Append("<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"yes\"?>");
            sb.Append(dataSet.GetXml().Replace("<NewDataSet>", "").Replace("</NewDataSet>", ""));
            if (!File.Exists(FilePath))
            {
                File.WriteAllText(FilePath, sb.ToString(), Encoding.Default);
            }
        }

    }
}
